<?php
/**
 * File for the Student Class
 * 
 * $Rev:: 48            $:  Revision of last commit
 * $Author:: guy        $:  Author of last commit
 * $Date:: 2010-04-19 1#$:  Date of last commit
 * 
 * @package eesc
 */

/**
 * Require rules
 */
require_once BASE_URI . '/lib/Rule.class.php';

/**
 * Class describing details of a student (regID, options etc..)
 * 
 * Student provides detail on a single student registration
 * Used to temporarily construct a object profile of the student
 * and access/store their choices.
 * 
 * @package eesc
 * @subpackage classes
 */
class Student {
	
	private $USN;					// Student unique registration ID
	
	//private $regID;					// Student unique registration ID
	 
	//private $option;				// selects which option group a stundent is in (Half, A or B for at this time)
	
	private $choices = array();		// Student choice as an array;
	
	private $status;				// The students status should be considered "unregistered","pending approval" or "approved"


	private $db;

	/**
	 * Contructor
	 * 
	 * @param integer $USN
	 * @return object
	 */
	public function __construct($USN) {
		
		$this->db = new Database();
		
		$this->db->connect();
		
		$this->USN = $USN;
	
		/* If their status is not set, set it to unregistered */
		if ($this->getStatus() === NULL){
			$this->db->insert("Student", array("USN" => $this->USN,"status" => "unregistered"));
		}
		
		if ($array_temp = $this->db->select("choice", "courseID", "WHERE USN = " . $this->USN)) {
			foreach ($array_temp as $row) {
				$this->choices[] = $row["courseID"];
			}
		}
		
		$this->getStatus();
		
	}
	
	/**
	 * Returns a students array of choices
	 * 
	 * @return array $this->choices
	 */
	public function getChoices() {
		return $this->choices;
	}
	
	/**
	 * Adds a courseID to the choices table for this student
	 * 
	 * @param object $courseID
	 * @return bool
	 */
	public function addChoice($courseID) {
		return $this->db->insert("choice", array("USN" => $this->USN,"courseID" => $courseID));
	}
	
	/**
	 * Removes all rows with courseID from the choices table for this student
	 * 
	 * @param object $courseID
	 * @return bool
	 */
	public function removeChoice($courseID) {
		return $this->db->delete("choice", array("USN" => $this->USN,"courseID" => $courseID));
	}
	
	/**
	 * Clears all the rows in Choices for this student
	 * 
	 * @return bool
	 */
	public function clearChoices() {
		return $this->db->delete("choice",array("USN" => $this->USN));
	}
	
	/**
	 * Check the validity of the choice by constructing a new rule object at evaluating
	 * their choices against this rule object
	 * 
	 * @return bool
	 */
	public function checkRegistration() {
		
		$rule = new Rule(NULL,$this->tripos);
		return $rule->evaluate($this->choices);
	}
	
	/**
	 * Get the students current status from the database and set the object var
	 * 
	 * @return String $this-status
	 */
	public function getStatus() {
		 if ($data = $this->db->select("Student", "status", "WHERE USN = " . $this->USN)) {
		 	$this->status = $data[0]["status"];
			return $this->status;
		 } else {
		 	return false;
		 }
	}
	
	/**
	 * Set the students registration status
	 * 
	 * @param String $status
	 * @return bool
	 */
	public function setStatus($status) {
		if ($this->db->update("Student",array("USN" => $this->USN),array("status"=>$status)))
		{
			$this->status = $status;
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 
	 * @param object $tripos
	 * @return bool
	 */
	/*public function setTripos($tripos) {
		
		return $this->db->update("Student",array("USN" => $this->USN),array("tripos"=>$tripos));
		
	}*/
	
	/**
	 * Return the students tripos number
	 * 
	 * @return bool
	 */
	/*public function getTripos()
	{

		$data = $this->db->select("Student", "tripos", "WHERE USN = " . $this->USN);
		if (($data[0]["tripos"] != "") OR ($data[0]["tripos"] != NULL)) {
			$this->tripos = $data[0]["tripos"];
			return $this->tripos;
		} else {
			return false;
		}
	}*/
	
	public function __get($column) {
		$data = $this->db->select("Student", $column, "WHERE USN = " . $this->USN);
		if ($data OR ($data[0][$column] != "") OR ($data[0][$column] != NULL)) {
			return $data[0][$column];
		} else {
			return false;
		}
	}
	
	public function __set($column, $entry) {
		return $this->db->update("Student",array("USN" => $this->USN),array($column=>$entry));
	}
	
	
	/**
	 * Return a list of Value relating to the student
	 * 
	 * @param array $columns [optional]
	 * @return array $details
	 */
	public function getDetails($columns = "*") {
		$tis = new TIS();
		$details = $tis->select("tbl_students", $columns, "WHERE USN = " . $this->USN);
		$details[0]["choices"] = $this->titleChoices();
		$details[0]["label"] = $this->USN;
		return $details[0];
	}
	
	
	/**
	 * 
	 * @param array $columns [optional]
	 * @return String $html
	 */
	public function getTableDetails($columns = "*") {
		
		$data = $this->getDetails($columns);
		$html = "";
		$html .= "<table>";
			
		$html .= "<thead><tr>";
		foreach (array_keys($data) as $heading) {
				$html .= "<th>$heading</th>";
		}
		$html .= "</tr></thead><tbody>";
		//foreach ($data as $row) {
			$html .= "<tr>";
			foreach ($data as $val) {
				$html .= "<td>$val</td>";
			}
			$html .= "</tr>";
		//}
		$html .= "</tbody></table>";
		
		return $html;
	}

	/**
	 * Returns a chunk of html with the form including choices of papers
	 * 
	 * @return String $html 
	 */
	public function choiceForm() {
		
		$tis = new TIS();
		$courses = $tis->getCourses($this->tripos);
		
		$tis->getAcademicYear();
		
		if ($this->status == 'unregistered') {
			$disabled = "";
			$html = "<p>Tripos: <span id=\"tripos\">{$this->tripos} <button class=\"ui-state-default ui-corner-all\" id=\"changeTripos\" name=\"changeTripos\">Change</button></span></p>";
		} else {
			$disabled = "disabled=\"disabled\"";
		}
		
		

		$html .= "<form id=\"choice\" action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">";
		$html .= "<table>";
		$html .= "<thead><tr><th></th><th>Course Name</th><th>Web Link (new window)</th></tr></thead>";
		$html .= "<tbody>";
		foreach ($courses as $course) {
			if(in_array($course["A_INT_CourseID"], $this->choices)) {
				$selected = " checked=\"checked\" ";
			} else {
				$selected = "";
			}
			$html .= "<tr><td><input $disabled class=\"choice\" type=\"checkbox\" name=\"choice\" value=\"{$course['A_INT_CourseID']}\" id=\"choice-{$course['A_INT_CourseID']}\" $selected /></td><td><label for=\"choice-{$course['A_INT_CourseID']}\">".htmlentities($course["STR_SubjectName"])." </label></td><td><a target=\"_new\" href=\"{$course["TXT_CourseURL"]}\">{$course["TXT_CourseURL"]}</a></td></tr>";
		}
		$html .= "</tbody>";
		$html .= "</table>";
		
		if ($this->status == 'unregistered') {
			
			// Show the students current registration with their
			
			$html .= "<button class=\"ui-state-default ui-corner-all\" id=\"checkRegistration\">Check Registration</button>";
			$html .= "<input type=\"hidden\" name=\"status\" value=\"pending\" />";
			$html .= "<input class=\"ui-state-default ui-corner-all\" type=\"submit\" id=\"submit\" value=\"Submit Registration\" />";

		// If the status is pending then we show one option to undo their registration
		} elseif ($this->status == 'pending') {
			$html .= "<button class=\"ui-state-default ui-corner-all\" id=\"checkRegistration\">Check Registration</button>";
			$html .= "<input type=\"hidden\" name=\"status\" value=\"unregistered\" />";
			$html .= "<input class=\"ui-state-default ui-corner-all\" type=\"submit\" id=\"submit\" value=\"Undo Registration\"/>";

		// If we fully registered then we show nothing 
		} elseif ($this->status == 'registered') {
			/**
			 * @todo: Perhaps have an email option to their DoS to under their registration
			 */
		}
		
		$html .= "</form>";
				
		return $html;
	}
	
	/**
	 * Get the formto show a dos a paticular students (USN) choices
	 * 
	 * @param object $USN
	 * @return String $html 
	 */
	public function dosForm($USN) {

		$html = "<form id=\"choice\" action=\"{$_SERVER['PHP_SELF']}?USN=$USN\" method=\"post\">";
		$html .= "<table>";
		$html .= "<thead><tr><th>ID</th><th>Choices</th></tr></thead>";
		$html .= "<tbody>";
		foreach ($this->associativeChoices() as $choiceid => $title) {
			$title = htmlentities($title);
			$html .= "<tr><td>$choiceid</td><td>$title</td></tr>";
		}
		$html .= "</tbody>";
		$html .= "</table><br />";
		
		if ($this->status == 'unregistered') {
			$html .= "<p>Awaiting student submission</p>";
			
		} elseif ($this->status == 'pending') {
			$html .= "<button class=\"ui-state-default ui-corner-all\" id=\"checkRegistration\">Check Registration</button>";
			$html .= "<input type=\"hidden\" name=\"status\" value=\"unregistered\" />";
			$html .= "<input class=\"ui-state-default ui-corner-all\" type=\"submit\" id=\"submit\" value=\"Reject Registration\"/>";
			$html .= "</form><form id=\"choice\" action=\"{$_SERVER['PHP_SELF']}?USN=$USN\" method=\"post\">";
			$html .= "<input type=\"hidden\" name=\"status\" value=\"registered\" />";
			$html .= "<input class=\"ui-state-default ui-corner-all\" type=\"submit\" id=\"submit\" value=\"Approve Registration\"/>";
			
		} elseif ($this->status == 'registered') {
			$html .= "<input type=\"hidden\" name=\"status\" value=\"pending\" />";
			$html .= "<input class=\"ui-state-default ui-corner-all\" type=\"submit\" id=\"submit\" value=\"Undo Registration\"/>";
		}
		
		$html .= "</form>";
				
		return $html;
	}
	
	/**
	 * Return a list of the triposes available to take in a (radio) choice form
	 * 
	 * @return String $html 
	 */
	public function triposForm() {
		$tis = new TIS();
		
		$triposes = $tis->getTriposes();
		
		//$triposesKeys = array_keys($triposes);
		
		$html = "<form style=\"display:inline\" id=\"tripos\" action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">";
		$html .= "<select name=\"tripos\">";
		foreach ($triposes as $tripos) {
			$html .= "<option name=\"tripos\" value=\"{$tripos["A_INT_TriposGroupID"]}\" id=\"tripos-{$tripos["A_INT_TriposGroupID"]}\" >{$tripos["STR_TriposGroupName"]}</option>";
		}
		$html .= "</select>";
		$html .= "<input class=\"ui-state-default ui-corner-all\" type=\"submit\" id=\"submit\" value=\"submit\" />";
		$html .= "<input class=\"ui-state-default ui-corner-all\" type=\"hidden\" id=\"action\" name=\"action\" value=\"select\" />";
		
		$html .= "</form>";
		
		return $html;
	}
	
	/**
	 * Retrieve an array of the student's choices (with titles)
	 * 
	 * @return array 
	 */
	public function associativeChoices() {
		
		if (count(($this->choices)) === 0) {
			return $this->choices;
		}
		
		$tmp_choices = array();
		$tis = new TIS();
		
		$allCourses = $tis->getCourses(NULL,array("title as STR_SubjectName"));
		
		foreach($this->choices as $choice) {
			$tmp_choices[$choice] = $allCourses[$choice]["STR_SubjectName"];
		}
		
		return $tmp_choices;
	}
	
	/**
	 * Gets a list of just the choice titles
	 * 
	 * @return array $titleChoices
	 */
	public function titleChoices() {
		if (count(($this->choices)) === 0) {
			return $this->choices;
		}
		
		$titleChoices = array();
		$tis = new TIS();
		
		foreach($this->choices as $choice) {
			$course = $tis->select("tbl_courses",array("STR_SubjectName"),"WHERE A_INT_CourseID = " . $choice);
			$titleChoices[] = $course[0]["STR_SubjectName"];
		}
		
		return $titleChoices;
	}
	
	/**
	 * Returns an html string table of the student's choices
	 * 
	 * @return string $html		returns a string of html (a table) 
	 */
	public function tableChoices($type) {
		
		include "globals.php";
		
		$html = "";

		$student_choices = $this->associativeChoices();
		
		switch($type) {
			case "table":
				$html .= "<table>";
								
				$html .= "<tr>";
				foreach (array_keys($student_choices[0]) as $heading) {
					$html .= "<th>$heading</th>";
				}
				$html .= "</tr><tr>";
				
				foreach ($student_choices as $choice) {
					foreach ($choice as $data) {
						$html .= "<td>$data</td>";
					}
					$html .= "</tr>";
				}
				$html .= "</table>";
				break;
			default:
				$html .= "<p>No type choses</p>";
				break;	
		}
		return $html;
	}
}
